package com.ray.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.CustomerBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.PutOutServiceNew;
import com.ray.service.ThirdPartyViewService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

//客户或外协登录
@Controller
@RequestMapping("/thirdPartyView")
public class ThirdPartyViewController {

	@Autowired
	ThirdPartyViewService thirdPartyViewService;

	@RequestMapping("/{methodName}")
	@ResponseBody
	private Map<String, Object> addAirFlowControl(HttpServletRequest request,HttpServletResponse response,
			@PathVariable("methodName") String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException {
		Map<String, Object> requestParamMap = getRequestAllParam(request);
		requestParamMap.put("request", request);
		requestParamMap.put("response", response);
		Method targetMethod = thirdPartyViewService.getClass().getMethod(urlMethodMappingMap.get(methodName), Map.class);
		Object object=null;
		try {
			 object=targetMethod.invoke(thirdPartyViewService, requestParamMap);
		}catch(InvocationTargetException e) {
			if(e.getCause().getClass()== IllegalArgumentException.class || e.getCause().getClass() ==ServiceException.class) {
				e.printStackTrace();
				Assert.notNull(null,e.getTargetException().getMessage());
			}else {
				e.printStackTrace();
				return ResponseUtil.ResponseError("网络不好，请稍等。。。。");
			}
		}
		return ResponseUtil.ResponseResult(object);
	}

	/**
	 *  方法名和请求路径映射
	 */
	private static  Map<String, String> urlMethodMappingMap;
	static{
		urlMethodMappingMap=new HashMap<String,String>();
		urlMethodMappingMap.put("login", "login");//登录
		urlMethodMappingMap.put("getAlreadySaveProcedure", "getAlreadySaveProcedure");//查询工艺结构树
		urlMethodMappingMap.put("getAllOrderSearchWhere", "getAllOrderSearchWhere");//获取订单四要素
		urlMethodMappingMap.put("getOrderMaterialParchaseAndInCountTime", "getOrderMaterialParchaseAndInCountTime");//获取物料采购和入库时间
		urlMethodMappingMap.put("getProductionDepartment", "getProductionDepartment");//获取生产部门
		urlMethodMappingMap.put("getDeptNoMegerWorkShop", "getDeptNoMegerWorkShop");//获取部门下未合并的车间
		urlMethodMappingMap.put("getOrderMain", "getOrderMain");//订单查询主信息
		urlMethodMappingMap.put("queryOrderPageDateil", "queryOrderPageDateil");//明细查询方法
		urlMethodMappingMap.put("getOrderApprovalImg", "getOrderApprovalImg");//获取订单审核图片
		urlMethodMappingMap.put("getOrderInfoDetail", "getOrderInfoDetail");//获取订单明细信息
		urlMethodMappingMap.put("getAleradyReportProcedure", "getAleradyReportProcedure");//获取已经报工的信息
		urlMethodMappingMap.put("getProcedureExpectDate", "getProcedureExpectDate");//获取预计生产日期
		urlMethodMappingMap.put("getProcedureReport", "getProcedureReport");//获取工序报工情况
		urlMethodMappingMap.put("getPutoutOrder", "getPutoutOrder");//获取外协订单外协情况
		urlMethodMappingMap.put("getOrderMaterialReport", "getOrderMaterialReport");//获取订单物料报工情况
		urlMethodMappingMap.put("loginOut", "loginOut");//退出登录
		urlMethodMappingMap.put("getMaterialSelectData", "getMaterialSelectData");//客户查看现货表
		urlMethodMappingMap.put("queryMaterialCompany", "queryMaterialCompany");//客户查看现货表
	}
	
	
	private  static Map getRequestAllParam(HttpServletRequest request) throws ServiceException {
		Map map=new HashMap();
		Enumeration enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			map.put(paraName, request.getParameter(paraName));
		}
		/**
		 * 如果不是登录接口，需要设置登录参数
		 */
		if(null == map.get("isLogin")) {
			CustomerBean  customerBean =(CustomerBean) request.getSession().getAttribute("thirdPartyViewLogin");
			if(customerBean==null) {
				Assert.notNull(null,"客户未登录或已掉线");
			}
			map.put("loginCustomerCompany", customerBean.getCompany());
			map.put("loginCustomerName", customerBean.getCustomerName());
			map.put("loginCustomerId", customerBean.getId());
			map.put("loginCompany", customerBean.getCompany());
		}
		return map;
	}
	
	
	
}
